<template>
  <div>
    <el-form :inline="true" class="demo-form-inline" :model="cateForm">
      <el-form-item label="一级分类">
        <el-select
          :disabled="show"
          placeholder="请选择"
          v-model="cateForm.category1Id"
          @change="selectChange1"
        >
          <el-option
            :label="product1.name"
            :value="product1.id"
            v-for="product1 in list1"
            :key="product1.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select
          :disabled="show"
          placeholder="请选择"
          v-model="cateForm.category2Id"
          @change="selectChange2"
        >
          <el-option
            :label="product2.name"
            :value="product2.id"
            v-for="product2 in list2"
            :key="product2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="show"
          placeholder="请选择"
          v-model="cateForm.category3Id"
          @change="selectChange3"
        >
          <el-option
            :label="product3.name"
            :value="product3.id"
            v-for="product3 in list3"
            :key="product3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props: ["show"],
  data() {
    return {
      cateForm: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
      list1: [],
      list2: [],
      list3: [],
    };
  },
  mounted() {
    this.getCategory1List();
  },

  methods: {
    async getCategory1List() {
      let result = await this.$API.attr.reqCategory1List();
      if (result.code == 200) {
        this.list1 = result.data;
      }
    },

    // 一级分类商品发生改变时
    async selectChange1() {
      // 清空二级id
      this.cateForm.category2Id = "";
      this.cateForm.category3Id = "";
      this.list2 = [];
      this.list3 = [];
      //获取一级id发送请求获取二级数据
      let id = this.cateForm.category1Id;
      this.$emit("getCategoryList", { categoryId: id, level: 1 });
      let result = await this.$API.attr.reqCategory2List(id);
      if (result.code == 200) {
        this.list2 = result.data;
      }
    },

    async selectChange2() {
      this.cateForm.category3Id = "";
      this.list3 = [];
      let id = this.cateForm.category2Id;
      this.$emit("getCategoryList", { categoryId: id, level: 2 });
      let result = await this.$API.attr.reqCategory3List(id);
      if (result.code == 200) {
        this.list3 = result.data;
      }
    },

    async selectChange3() {
      let id = this.cateForm.category3Id;
      this.$emit("getCategoryList", { categoryId: id, level: 3 });
    },
  },
};
</script>

<style>
</style>